/**
 * CollectionHelper
 *
 * Released under Apache License, Version 2.0.
 */

package bsp.helper;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import bsp.helper.objects.Pair;
import bsp.helper.objects.Triplet;

public class CollectionHelper
{
	protected CollectionHelper()
	{
		/* empty by design */
	}
	
	/**
	 * This method can be used to convert an ArrayList that contains strings
	 * into a String object.
	 * 
	 * @param list ArrayList to be converted.
	 * @param fieldSep Field separator.
	 * @return String
	 */
	public static String serializeArrayListHavingStringValue(ArrayList<String> list, String fieldSep)
	{
		StringBuffer outStr = new StringBuffer(5000);
		
		if ( list != null ) {
			for ( int i = 0; i < list.size(); i++ ) {
				if ( i > 0 ) {
					outStr.append(fieldSep);
				}
				
				outStr.append(list.get(i));
			}
		}
		
		return outStr.toString();
	}
	
	/**
	 * This method can be used to convert an ArrayList that contains integers
	 * into a String object.
	 * 
	 * @param list ArrayList to be converted.
	 * @param fieldSep Field separator.
	 * @return String
	 */
	public static String serializeArrayListHavingIntegerValue(ArrayList<Integer> list, String fieldSep)
	{
		StringBuffer outStr = new StringBuffer(5000);
		
		if ( list != null ) {
			for ( int i = 0; i < list.size(); i++ ) {
				if ( i > 0 ) {
					outStr.append(fieldSep);
				}
				
				outStr.append(list.get(i).toString());
			}
		}
		
		return outStr.toString();
	}
	
	/**
	 * This method can be used to convert an ArrayList with date objects
	 * into a single String object.
	 * 
	 * @param list ArrayList to be converted.
	 * @param fieldSep Field separator.
	 * @return String
	 */
	public static String serializeArrayListHavingDateValue(ArrayList<Date> list, String fieldSep)
	{
		StringBuffer outStr = new StringBuffer(5000);
		
		if ( list != null ) {
			for ( int i = 0; i < list.size(); i++ ) {
				if ( i > 0 ) {
					outStr.append(fieldSep);
				}
				
				outStr.append(list.get(i).toString());
			}
		}
		
		return outStr.toString();
	}
	
	/**
	 * Convert a String into an array of strings.
	 * 
	 * @param src String to be converted.
	 * @param separator Separator.
	 * @return String[] Array of strings.
	 */
	public static String[] convertStringVectorToStringArray(Vector<String> src)
	{
		String[] ret = null;
		
		if ( src != null && src.size() > 0 ) {
			int size = src.size();
			ret = new String[size];
			
			for ( int i = 0; i < size; i++ ) {
				ret[i] = new String(src.get(i));
			}
		}
		
		return ret;
	}
	
	/**
	 * This method can be used to convert a HashMap containing strings
	 * into a String object.
	 * 
	 * @param map HashMap to be converted.
	 * @param fieldSep Field separator.
	 * @param recordSep Record separator.
	 * @return String
	 */
	public static String serializeHashMapHavingStringValue(HashMap<String, String> map, String fieldSep, String recordSep)
	{
		StringBuffer str = new StringBuffer(5000);
		
		if ( map != null ) {
			Iterator<String> iterator = map.keySet().iterator();
			while( iterator. hasNext() ){
				String key = iterator.next();
				String st = map.get(key);
				
				str.append(key).append(fieldSep).append(st).append(recordSep);
			}
		}
		
		return str.toString();
	}
	
	/**
	 * This method is used to convert a HashMap into a String object.
	 * Each element in the HashMap is a StringPair.
	 * 
	 * @param map HashMap to be converted.
	 * @param fieldSep Field separator.
	 * @param recordSep Record separator.
	 * @return String
	 */
	public static String serializeHashMapHavingStringPair(HashMap<String, Pair<String, String>> map, String fieldSep, String recordSep)
	{
		StringBuffer str = new StringBuffer(5000);
		
		if ( map != null ) {
			Iterator<String> iterator = map.keySet().iterator();
			while( iterator. hasNext() ){
				String key = iterator.next();
				Pair<String, String> st = map.get(key);
				
				str.append(key).append(fieldSep);
				str.append(st.one).append(fieldSep);
				str.append(st.two).append(recordSep);
			}
		}
		
		return str.toString();
	}
	
	/**
	 * Serialize hash map containing triplet object.
	 * 
	 * @param map
	 * @param fieldSep
	 * @param recordSep
	 * @return
	 */
	public static String serializeHashMapHavingStringTriplet(HashMap<String, Triplet<String, String, String>> map, String fieldSep, String recordSep)
	{
		StringBuffer str = new StringBuffer(5000);
		
		if ( map != null ) {
			Iterator<String> iterator = map.keySet().iterator();
			while( iterator.hasNext() ){
				String key = iterator.next();
				Triplet<String, String, String> st = map.get(key);
				
				str.append(st.one).append(fieldSep).append(st.two).append(fieldSep).append(st.three).append(recordSep);
			}
		}
		
		return str.toString();
	}
}

/* end */
